package m1;

import org.apache.rocketmq.client.consumer.DefaultMQPullConsumer;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

public class Consumer {
    public static void main(String[] args) throws Exception {
        // 新建消费者实例
        DefaultMQPushConsumer c = new DefaultMQPushConsumer("consumer-group-1");
        // 设置 nameserver 地址
        c.setNamesrvAddr("192.168.64.141:9876");
        // 设置消息监听器
        c.setMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
                for (MessageExt mext: msgs) {
                    String s = new String(mext.getBody());
                    System.out.println("收到： "+s);
                }
                //返回消息处理状态： 成功、稍后重新消费
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                // return ConsumeConcurrentlyStatus.RECONSUME_LATER;
            }
        });
        // 设置接收的主题和标签
        c.subscribe("Topic1", "TagA || TagB || TagC");
        // 启动
        c.start();
    }
}